import workManagement from "@/api/workManagement/index";
const state = {
  flowInfo:{},
  btnList:[],
  current_state_id:"",
  enable_dist:false
};
const mutations = {
  SET_FLOWINFO: (state, flowInfo) => {
    state.flowInfo = flowInfo
  },
  SET_BUTTONLIST: (state, btnList) => {
    state.btnList = btnList
  },
  SET_CURRENTSTATEID: (state, current_state_id) => {
    state.current_state_id = current_state_id
  },
  SET_ENABLE_DIST: (state, enable_dist) => {
    state.enable_dist = enable_dist
  },
};
const actions = {
  getWorkInfo({ commit,dispatch }, row){
    return new Promise((resolve, reject)=> {
      workManagement.getDetailInfo(row.ticket).then(res=>{

        let flowInfo = res.data.value;

        commit("SET_FLOWINFO",flowInfo);
        commit("SET_ENABLE_DIST",flowInfo.state_info.enable_dist);
        workManagement.getTransitions(row.ticket).then(result=>{
          let btnList = result.data.value;
          if(flowInfo.state_info.enable_retreat==2&btnList.length > 0){
            btnList.push({transition_name:'部门审批',desc:"该文件可在全部人员之间流转",isForwardAll:true,select_checkbox:true})
          }else if(flowInfo.state_info.enable_retreat==1&&btnList.length > 0){
            btnList.push({transition_name:'部门审批',desc:"该文件可在本单位其他人员之间流转",isForward:true,select_checkbox:true})
          }
          if(flowInfo.state_info.enable_add&&btnList.length > 0 &&
            btnList[0].in_add_node === false &&
            btnList[0].is_accept === false){
            btnList.push({transition_name:'加签',desc:"该文件可转交给任何人签署意见",isAdd:true,select_checkbox:true})
          }
          commit("SET_BUTTONLIST",btnList);

          dispatch("suggestion/setEnable", btnList.length>0||row.is_read,{ root: true });

          resolve(res)
        })
      })
    }).catch(()=>{
      reject()
    });
  },
  getFlowsteps({ commit }, row){
    return new Promise((resolve, reject)=> {
      workManagement.getFlowsteps(row.ticket).then(res => {
        let list = res.data.value.filter(item=>{
          return item&&item.state_flow_log_list.length>0 ||res.data.current_state_id==item.state_id
        });
        commit("SET_CURRENTSTATEID",res.data.current_state_id);
        resolve(list)
      })
    }).catch(()=>{
      reject()
    });
  }
};
export default {
  namespaced: true,
  state,
  mutations,
  actions
};
